#include <stdio.h> 
#include <stdlib.h>
#include <math.h> 

// ****** SPEEDUP ****** make all floats instead of double? 

int main(int argc, char *argv[])
{
    if(argc != 3)
    {
         printf("Not enough or too many arguments\nExpecting 3");  
        return 1; 
    }

    unsigned int N; 
    int A; 
    N = atoi(argv[1]); 
    A = atoi(argv[2]);
    // Make our 2D array for the grid. 
    // ****** SPEEDUP ****** make the 2D array a 1D array and
    // take care of the underlying framework manually
	/*
    double ** grid = (double**)malloc(sizeof(double*)*N);	//new double*[N];
    for(int i=0; i<N; i++)
        grid[i] = (double*)malloc(sizeof(double)*N);		//new double[N];  
    for(int i=0; i<N; i++)
        for(int j=0; j<N; j++)
            grid[i][j] = 0; 
	*/
    double limit = N*N/3; 
    //unsigned int normalLimit = limit/(double(N)/10); 
    unsigned int normalLimit = N/10; 

    double thetaStep = (double)normalLimit/(double)limit;

    int wait; 

 
    double xCord = 0; 
    double yCord = 0; 
    double magnitude = 0; 
    double theta = 0; 


    double maxMag = 0; 
    double maxX = 0; 
    double maxY = 0;     

    for(double t=0; t<normalLimit; t+=thetaStep)
    {
        // ****** SPEEDUP ****** Simplify this math? 
        //xCord = A*(cos(t)+t*sin(t));        
        theta = t;  //*M_PI/180; 
        xCord = A*(cos(theta)+theta*sin(theta));        
        yCord = A*(sin(theta)-theta*cos(theta)); 
        magnitude = sin(theta)*sin(theta);
		if(magnitude > maxMag)
		{
			maxMag = magnitude; 
			maxX = xCord; 
			maxY = yCord; 
		}        


        // Choosing highest value in cell
        // Simplify redundant math
        //if(magnitude > grid[(int)xCord+N/2][(int)yCord+N/2])
            //grid[(int)xCord+N/2][(int)yCord+N/2] = magnitude; 
        
    }



    printf("\nMax Z(x,y) is: %f at X = %f  Y = %f \n\n", maxMag, maxX, maxY);

   
    return 0; 
}
    
